package com.example.opengl.render.triangle

import android.opengl.GLES20
import android.opengl.GLSurfaceView
import com.example.opengl.render.base.IDrawer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class TriangleRender():GLSurfaceView.Renderer {
    private var drawers:MutableList<IDrawer> = mutableListOf()
    fun  addDrawerRender(iDrawer: IDrawer){
        drawers.add(iDrawer)
    }

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        GLES20.glClearColor(0.1f, 0.2f, 0.3f, 0.4f)
        val texture = IntArray(1)
        GLES20.glGenTextures(1, texture, 0) //生成纹理
        drawers.forEach {
            it.setTextureID(texture[0])
        }
    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
        drawers.forEach {
            it.setWorldSize(width,height)
        }
    }

    override fun onDrawFrame(gl: GL10?) {
       drawers.forEach {
           it.draw()
       }
    }
}